package cn.itsource.pethome.user.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import cn.itsource.pethome.base.domain.BaseQuery;
import cn.itsource.pethome.base.domain.PageResult;
import cn.itsource.pethome.base.util.RedisService;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author: hong
 * @Description:
 * @Date: Created in 12:53 2022/2/25
 * @Modified: By:
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisService redisService;

    /**
     * 手机号校验
     * @param phone
     */
    @Override
    public void checkPhone (String phone) {
        //校验手机号
        Validator.validateMobile (phone,"手机号格式有误,请确认后重新输入");

        //判断手机号是否存在,如果 true ,存在,抛异常
        if(userMapper.findPhoneExist(phone)) throw new RuntimeException ("该手机号已注册");

    }



    /**
     * 获取验证码
     * @param phone
     */
    @Override
    public void sendPhoneCode (String phone) {
        //校验手机号
        Validator.validateMobile (phone,"手机号格式有误,请确认后重新输入");
        //判断key是否存在,如果存在说明用户五分钟内已经获取过验证码直接给用户返回
        String existcode = redisService.getKeyObjectValue (phone, String.class);
        if(Objects.isNull (existcode)){
            //1.1不存在,生成code ,完成发送
            String code = RandomUtil.randomInt (1000,9999)+"";
            //1.2将code存入redis
            redisService.setStringKeyAndValue (phone,code,5);
            //1.3发送短信
            log.info ("短信已发送,接受的手机号为{},短信的code码为:{}",phone,code);
        }else {
            //2.存在,直接取出code发送
            log.info ("短信已发送,接受的手机号为{},短信的code码为:{}",phone,existcode);
        }

    }

    /**
     * 前台用户注册的业务逻辑
     * @param user
     */
    @Override
    public void insert (User user) {
        //判断两次密码是否一致
        if (!Objects.equals(user.getPassword(),user.getConfigPassword())) throw new RuntimeException ("两次密码输入不一致,请确认后重新输入");

        //判断用户当前code是否存在
        String existCode = redisService.getKeyObjectValue (user.getPhone (), String.class);
        //如果code不相等,或者不存在
        if (!Objects.equals (user.getCode(),existCode)){
            throw new RuntimeException ("当前验证码不存在或已过期,请从新获取");
        }
        //初始化数据,加密密码,并且设置盐值,设置创建时间
        user.init ();
//        String Password  = user.getPassword();
//        user.setSalt(RandomUtil.randomNumbers(4));
//        String s = MD5.create().setSalt(user.getSalt().getBytes()).digestHex(Password);
//        user.setPassword(s);
        //存入数据库
        userMapper.insertSelective (user);
    }

    @Override
    public void update (User user) {

    }

    @Override
    public void delete (Long id) {

    }

    @Override
    public void deleteBatch (List<Long> ids) throws Exception {

    }

    @Override
    public PageResult findPage (BaseQuery query) {
        return null;
    }

    @Override
    public List<User> findAll () {
        return null;
    }

    @Override
    public User findOne (Long id) {
        return null;
    }


    /**-------------------------------------------private -----------------------*/

}
